<?php

namespace app\admin\model;

use think\Model;

class MemberEarningDetail extends Model
{
    private static $instance = null;

    public function getAddTimeAttr($time)
    {
        return date('Y-m-d H:i:s', $time);
    }

    public static function getInstance()
    {
        //检测当前类属性$instance是否已经保存了当前类的实例
        if (self::$instance == null) {
            //如果没有,则创建当前类的实例
            self::$instance = new self();
        }
        //如果已经有了当前类实例,就直接返回,不要重复创建类实例
        return self::$instance;
    }

    /**
     * 单条查询
     * @param $where
     * @param string $field
     * @param false $lock
     * @return Member
     */
    public function getOne($where, $field = '*', $lock = false)
    {
        return $this->lock($lock)->field($field)->where($where)->find();
    }

    // 用户列表
    public function memberList($where, $page, $size, $field = [], $order = 'dinosaur_num')
    {
        $list = $this->field($field)
            ->where($where)
            ->order($order, 'desc')
            ->paginate($size, true, ['page' => $page])
            ->toArray();

        return $list['data'];
    }

    public function getAll($where, $field = '*')
    {
        return $this->field($field)->where($where)->select();
    }

    public function getListByPage($where, $page, $size, $field = [])
    {
        $list = $this->field($field)
            ->where($where)
            ->order('add_time', 'desc')
            ->paginate($size, true, ['page' => $page])
            ->toArray();

        return $list['data'];
    }

    public function getCount($where = [], $field = '*')
    {
        return $this->where($where)->count($field);
    }

    public function getList($num = 15)
    {
        $keywords = input('param.keywords', '');
        $vip_level = input('param.vip_level', '');
        $where = "";
        $param = [];
        if ($keywords) {
            $param['keywords'] = $keywords;
            $where .= "name like '" . $keywords . "%'";
        }
        if ($vip_level) {
            $param['vip_level'] = $vip_level;
            $where .= "vip_level =" . $vip_level;
        }

        $exchangeList = $this->where($where)
            ->order('id', 'DESC')
            ->paginate($num, false, ['query' => request()->param()]);
        if ($exchangeList) {
            $page = $exchangeList->render();// 获取分页显示
            return ['data' => $exchangeList, 'msg' => '数据查询成功', 'page' => $page];
        } else {
            return ['data' => '', 'msg' => '暂无数据', 'page' => ''];
        }
    }

    public function getSumByBetween($where, $field = '*')
    {
        return $this->whereBetweenTime('add_time', $where['start_time'], $where['end_time'])
            ->where(['pid' => $where['pid']])
            ->sum($field);
    }

    public function getSum($where, $field = '*')
    {
        return $this->where($where)->sum($field);
    }

    public function getValues($where = [], $field = '*')
    {
        return $this->where($where)->value($field);
    }

    public function updateDinosaur($where, $data)
    {
        return $this->where($where)->update($data);
    }
}
